﻿/**
* This method handle menu context.
* @param args Unused.
* @return Nothing.
* @exception error when ...
* @see exception
*/
$(document).ready(function () {
    $('div' || 'a' || 'h1' || 'h2' | 'h3').mousedown(function (e) {
        if (e.button == 2) {
            $.contextMenu({
                selector: 'div' || 'a' || 'h1' || 'h2' | 'h3',
                items: {
                    key: {
                        name: "Change language key value",
                        callback: $.noop
                    },
                    // <input type="texboxt">
                    LangVI: {
                        name: "Lang vi-VI",
                        type: 'text',
                        value: getDataValue("vi-VN")
                    },
                    // <input type="texboxt">
                    LangUS: {
                        name: "Lang en-US",
                        type: 'text',
                        value: getDataValue("en-US")
                    },
                    // <input type="label">
                    SubmitBtn: {
                        name: "",
                        type: 'button',
                        value: "Save/Lưu",
                        events: {
                            click: function (e) {
                                // add some fancy key handling here?
                                var str = $('input:text[name=context-menu-input-LangUS]').val();
                                alert(str);
                            }
                        }

                    }
                },
                events: {
                    show: function (opt) {
                        // this is the trigger element
                        var $this = this;
                        // import states from data store 
                        //$.contextMenu.setInputValues(opt, $this.data());
                        // this basically fills the input commands from an object
                        // like {name: "foo", yesno: true, radio: "3", …}
                    },
                    hide: function (opt) {
                        // this is the trigger element
                        var $this = this;
                        // export states to data store
                        //$.contextMenu.getInputValues(opt, $this.data());
                        // this basically dumps the input commands' values to an object
                        // like {name: "foo", yesno: true, radio: "3", …}
                    }
                }
            });
        }
    });

});
/**
* This method get data from server by ajax jQuery.
* @param args Unused.
* @return Nothing.
* @exception error when nothing get from server.
* @see exception
*/
function getDataValue(key) {    
//     var t = null;
//        if (window.getSelection) {
//            t = window.getSelection();
//        } else if (document.getSelection) {
//            t = document.getSelection();
//        } else if (document.selection) {
//            t = document.selection.createRange().text;
//        }
        //request ajax to get value from server
//        $.ajax({
//            type: 'GET', // define the type of HTTP verb we want to use (GET for our form)
//            url: '../library/process_lang.php', // the url where we want to GET
//            data: 'a', // our data object
//            dataType: 'text' // what type of data do we expect back from the server
//        }).done(function () {
//            $('input:text[name=context-menu-input-LangUS]').html(data);
        //        });
     var t = $.get(
				  "../library/process_lang.php",
				  { cmd: 0 },
				  function (data) {
				      return data;
				  }
				).responseText;
				  alert(t);
        return t;    
   
}
/**
* This method ....
* @param args Unused.
* @return Nothing.
* @exception error when nothing get from server.
* @see exception
*/
function getSelectionHtml() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}